package com.entrogy.controller;

import com.entrogy.bean.*;
import com.entrogy.service.MaintenanceService;
import com.entrogy.service.PermissionService;
import com.entrogy.service.UserService;
import com.entrogy.utils.AESUtil;
import com.entrogy.utils.constants.ErrorEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @ClassName MaintenanceController
 * @Description
 * @Author luoxingjiang
 * @Date 2019-12-11 上午 11:15
 * @Version 1.0
 **/
@RestController
@RequestMapping("/maintenance")
@Api(description = "运维管理控制层")
public class MaintenanceController {

    @Autowired
    private MaintenanceService maintenanceService;

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    @GetMapping(value = "/get-use-info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始日期yyyy-MM-dd", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期yyyy-MM-dd", dataType = "Date", paramType = "query"),
    })
    @ApiOperation(value = "按时段、学校查询功能使用情况（分为每天呈现）")
    public Msg getMessageSendInfo(String uid, Integer schoolCode, @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        MaintenanceUseOverViewEntity schoolUseInfoList = maintenanceService.getUseInfo(schoolCode, startDate, endDate);
        return Msg.success().add("schoolUseInfoList", schoolUseInfoList);
    }

    @GetMapping(value = "/list-maintenance-user")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
//            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query"),
    })
    @ApiOperation(value = "查询学校的运维管理人员")
    public Msg listMaintenanceUser(String uid, Integer currentPage, Integer pageSize) {
        String permissionCode = "maintenance:manager";
        if (currentPage == null || pageSize == null) {
            currentPage = 0;
            pageSize = 0;
        }
        PageHelper.startPage(currentPage, pageSize);
        List<UserEntity> userList = userService.listHasSomePermissionUserIdList(1, permissionCode);
        PageInfo<UserEntity> pageInfo = new PageInfo<>(userList);
        long totalCount = pageInfo.getTotal();
        return Msg.success().add("userList", userList).add("totalCount", totalCount);
    }

    @GetMapping(value = "/get-focus-info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始日期yyyy-MM-dd", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期yyyy-MM-dd", dataType = "Date", paramType = "query"),
    })
    @ApiOperation(value = "按时段、学校查询关注情况（分为每天呈现）")
    public Msg getFocusInfo(String uid, Integer schoolCode, @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        MaintenanceFocusOverViewEntity focusInfo = maintenanceService.getFocusInfo(schoolCode, startDate, endDate);
        return Msg.success().add("focusInfo", focusInfo);
    }

    @GetMapping(value = "/get-login-info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始日期yyyy-MM-dd", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期yyyy-MM-dd", dataType = "Date", paramType = "query"),
    })
    @ApiOperation(value = "按时段、学校查询登录情况（分为每天呈现）")
    public Msg getLoginInfo(String uid, Integer schoolCode, @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        MaintenanceLoginOverViewEntity loginInfo = maintenanceService.getLoginInfo(schoolCode, startDate, endDate);
        return Msg.success().add("loginInfo", loginInfo);
    }

    @GetMapping(value = "/get-register-info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始日期yyyy-MM-dd", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期yyyy-MM-dd", dataType = "Date", paramType = "query"),
    })
    @ApiOperation(value = "按时段、学校查询注册情况（分为每天呈现）")
    public Msg getRegisterInfo(String uid, Integer schoolCode, @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        MaintenanceRegisterOverViewEntity registerInfo = maintenanceService.getRegisterInfo(schoolCode, startDate, endDate);
        return Msg.success().add("registerInfo", registerInfo);
    }

    @PostMapping(value = "/maintenance-user-login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "passWord", value = "密码", dataType = "String", paramType = "query", required = true),
    })
    @ApiOperation(value = "运维人员登录")
    public Msg maintenanceUserLogin(String userName, String passWord) {
        if (!StringUtils.isNoneBlank(userName)) {
            return Msg.fail(ErrorEnum.E_90006, ErrorEnum.E_90006.getErrorMsg());
        }
        if (!StringUtils.isNoneBlank(userName)) {
            return Msg.fail(ErrorEnum.E_90007, ErrorEnum.E_90007.getErrorMsg());
        }
        UserEntity user = userService.getUserByMobile(userName);
        // 判断用户是否存在
        if (user == null) {
            return Msg.fail(ErrorEnum.E_90005, ErrorEnum.E_90005.getErrorMsg());
        }
        // 校验是否有权限
        String permissionCode = "maintenance:manager";
        boolean hasPermission = permissionService.hasAuthorization(user.getId(), permissionCode);
        if (!hasPermission) {
            return Msg.fail(ErrorEnum.E_502, ErrorEnum.E_502.getErrorMsg());
        }
        boolean compareResult = passWord.equals(AESUtil.decrypt(user.getPassword()));
        if (compareResult){
            // 查询是否属于小程序审核状态，13345678910为小程序审核时专用账号
            boolean test = false;
            if (user.getMobile().equals("12345678910")){
                test = true;
            }
            return Msg.success().add("uid", user.getId()).add("test", test);
        }
        return Msg.fail(ErrorEnum.E_90004, ErrorEnum.E_90004.getErrorMsg());
    }

    @GetMapping(value = "/get-school-info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
    })
    @ApiOperation(value = "查询学校信息")
    public Msg getSchoolInfo(String uid, Integer schoolCode) {
        MaintenanceSchoolEntity schoolInfo = maintenanceService.getSchoolInfo(schoolCode);
        return Msg.success().add("schoolInfo", schoolInfo);
    }

    @GetMapping(value = "/list-school-message")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query"),
    })
    @ApiOperation(value = "查询学校通知列表")
    public Msg listSchoolMessage(String uid, Integer schoolCode, Integer currentPage, Integer pageSize) {
        if (currentPage == null || pageSize == null) {
            currentPage = 0;
            pageSize = 0;
        }
        PageHelper.startPage(currentPage, pageSize);
        List<MaintenanceMessageEntity> messageList = maintenanceService.listSchoolMessage(schoolCode);
        PageInfo<MaintenanceMessageEntity> pageInfo = new PageInfo<>(messageList);
        long totalCount = pageInfo.getTotal();
        return Msg.success().add("messageList", messageList).add("totalCount", totalCount);
    }

    @GetMapping(value = "/list-school-parent")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "parentPhone", value = "家长电话", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentName", value = "家长姓名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query"),
    })
    @ApiOperation(value = "查询学校内的家长信息")
    public Msg listSchoolParent(String uid, Integer schoolCode, String parentPhone, String parentName, Integer currentPage, Integer pageSize) {
        if (currentPage == null || pageSize == null) {
            currentPage = 0;
            pageSize = 0;
        }
        PageHelper.startPage(currentPage, pageSize);
        List<MaintenanceParentEntity> parentList = maintenanceService.listParent(schoolCode, parentPhone, parentName);
        PageInfo<MaintenanceParentEntity> pageInfo = new PageInfo<>(parentList);
        long totalCount = pageInfo.getTotal();
        return Msg.success().add("parentList", parentList).add("totalCount", totalCount);
    }

    @GetMapping(value = "/list-school-teacher")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolCode", value = "学校id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherPhone", value = "教师电话", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "teacherName", value = "教师姓名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query"),
    })
    @ApiOperation(value = "查询学校内的教师信息")
    public Msg listSchoolTeacher(String uid, Integer schoolCode, String teacherPhone, String teacherName, Integer currentPage, Integer pageSize) {
        if (currentPage == null || pageSize == null) {
            currentPage = 0;
            pageSize = 0;
        }
        PageHelper.startPage(currentPage, pageSize);
        List<TeacherBasicInfoEntity> teacherList = userService.listTeacherForMaintenance(schoolCode, teacherPhone, teacherName);
        PageInfo<TeacherBasicInfoEntity> pageInfo = new PageInfo<>(teacherList);
        long totalCount = pageInfo.getTotal();
        return Msg.success().add("teacherList", teacherList).add("totalCount", totalCount);
    }
}
